# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from scrapy.pipelines.images import ImagesPipeline
from scrapy.conf import settings
from scrapy.exceptions import DropItem
import scrapy
import pymysql
import logging


class WallpaperPipeline(ImagesPipeline):
    def get_media_requests(self, item, info):
        yield scrapy.Request(url=str(item['image_path']))

    def item_completed(self, results, item, info):
        image_paths = [x['path'] for ok, x in results if ok]
        if not image_paths:
            raise DropItem("Item contains no images")
        # item['image_path'] = image_paths
        return item

    # 文件重命名
    # def file_path(self, request, response=None, info=None):
    #     pass


class MysqlPipeline(object):

    def __init__(self):
        # 创建数据库连接
        self.conn = pymysql.connect(host=settings.get('MYSQL_HOST'),
                                    port=3306,
                                    db=settings.get('MYSQL_DBNAME'),
                                    user=settings.get('MYSQL_USER'),
                                    passwd=settings.get('MYSQL_PASSWD'),
                                    charset='utf8',
                                    use_unicode=True)
        # 创建游标
        self.cursor = self.conn.cursor()

    def open_spider(self, spider):
        pass

    def process_item(self, item, spider):
        insert_sql = """
         INSERT INTO wallpaper(author,title,link,total_num,likes,unlike,image_path) VALUES(%s,%s,%s,%s,%s,%s,%s)
        """
        select_sql = """SELECT * FROM wallpaper WHERE image_path=%s"""
        try:
            # 查重
            self.cursor.execute(select_sql, item['image_path'])
            is_exit = self.cursor.fetchone()
            if not is_exit:
                self.cursor.execute(insert_sql,
                                    (item['author'], item['title'], item['link'], item['total_num'], item['likes'],
                                     item['unlike'], item['image_path']))
                self.conn.commit()
        except Exception as error:
            self.conn.rollback()
            print('Exception-->', error)
            logging.info(error)
        return item

    def close_spider(self, spider):
        self.cursor.close()
        self.conn.close()
